package com.yonyou.crm.cum.customer.ctrl;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.yonyou.crm.common.exception.CrmMessageException;
import com.yonyou.crm.common.page.entity.Page;
import com.yonyou.crm.common.rest.util.RequestUtil;
import com.yonyou.crm.common.rest.util.ResponseUtil;
import com.yonyou.crm.cum.customer.entity.CustomerVO;
import com.yonyou.crm.cum.customer.rmitf.ICustomerChartRmService;

@RestController
@RequestMapping(value = "/cum/customerchart")
public class CustomerChartCtrl {
	
	@Autowired
	private ICustomerChartRmService service;
	
	
	/**
	 * 获取部门、人员数据
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws CrmMessageException
	 */
	@RequestMapping(method = RequestMethod.GET)
	public Object getOwnerDatas(HttpServletRequest request,
			HttpServletResponse response) throws CrmMessageException {
		JSONObject paramObj = RequestUtil.getParamObject(request);
		Long deptId = null, userId = null;
		if(paramObj != null){
			deptId = StringUtils.isEmpty((String) paramObj.get("deptId"))?null:paramObj.getLong("deptId");
			userId = StringUtils.isEmpty((String) paramObj.get("userId"))?null:paramObj.getLong("userId");
		}	
		Object obj = service.getOwnerDatas(deptId, userId);
		return ResponseUtil.getObject(obj);
	}
	
	/**
	 * 获取客户数据
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws CrmMessageException
	 */
	@RequestMapping(value = "customers",method = RequestMethod.GET)
	public Object getCustomers(HttpServletRequest request,
			HttpServletResponse response) throws CrmMessageException {
		JSONObject paramObj = RequestUtil.getParamObject(request);
		int page = paramObj.getInt("page");
		int pageSize = paramObj.getInt("pageSize");
		Map<String, Object> searchMap = (Map<String, Object>) paramObj
				.get("searchMap");
		if (searchMap == null) {
			searchMap = new HashMap<String, Object>();
		}
		Page<CustomerVO> customers = service.getCustomers(new Page<CustomerVO>(page, pageSize), searchMap);
		return ResponseUtil.getObject(customers);
	}
	
	/**
	 * 获取图表数据
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws CrmMessageException
	 */
	@RequestMapping(value = "ratio",method = RequestMethod.GET)
	public Object getRatios(HttpServletRequest request,
			HttpServletResponse response) throws CrmMessageException {
		JSONObject paramObj = RequestUtil.getParamObject(request);
		Long deptId = StringUtils.isEmpty((String) paramObj.get("deptId"))?null:paramObj.getLong("deptId");
		Long userId = StringUtils.isEmpty((String) paramObj.get("userId"))?null:paramObj.getLong("userId");
		Object ratio = service.getRatio(deptId, userId);
		Map<String, Object> data = new HashMap<>();
		data.put("data" , ratio);
		return ResponseUtil.getObject(data);
	}
}
